<template>
  <yd-ls-box>
    <yd-ls-layout title="所患疾病"  :onfoot=false style="background-color: rgb(255,255,255)">
      <div class="yd-ls-height"></div>
      <yd-search v-model="searchSick"></yd-search>
      <yd-cell-item type="radio" v-for="(items,index) in searchData" :key="index">
        <span slot="left">{{ items.disease_name }}</span>
        <input slot="right" type="radio" :value="items.disease_name" v-model="picked"/>
      </yd-cell-item>
      <div class="yd-ls-height"></div>
    </yd-ls-layout>
    <yd-ls-button btnname="确定" @handle-button="confirm()"></yd-ls-button>
  </yd-ls-box>
</template>

<style>

</style>
<script>
import ydLsBox from "../../../components/yd-ls-box.vue";
import ydLsLayout from "../../../components/yd-ls-layout.vue";
import ydLsButton from "../../../components/yd-ls-button.vue";
import { mapGetters } from "vuex";
import api from "../../../axios/api";

export default {
  name: "regsick",
  data() {
    return {
      urls: "index.html#/reginfo",
      sickList: {},
      searchSick: '',
      picked: ""
    };
  },
  computed: {
    searchData() {
      var search = this.searchSick;
      if (search) {
        return this.sickList.filter(function(product) {
          return Object.keys(product).some(function(key) {
            return String(product[key]).toLowerCase().indexOf(search) > -1
          })
        })
      }

      return this.sickList;
    },
    ...mapGetters(["getMid"])
  },
  methods: {
    init() {
      //TODO: 初始化数据
      this.getSick();
    },
    getSick() {
      this.axios.get(api.reg_sick_list, {}).then(m => {
        let sick_data = m.data;
        this.sickList = sick_data.data;
        console.log(this.sickList)
      });
    },
    confirm() {
      if (!!!this.picked) {
        this.$dialog.notify({
          mes: "请先选择所患疾病!",
          timeout: 1500
        });
        return;
      }

      let pkid = "";
      let _this = this;
      this.sickList.forEach(function(value, key, arr) {
        if (_this.picked === value.disease_name) {
          pkid = value.pkid;
        }
      });
      this.$dialog.loading.open(api.default_loading_txt);
      this.axios.put(api.reg_sick_save + "/" + localStorage.openId + "/" + pkid)
        .then(m => {
          this.$dialog.loading.close()
          let sick = m.data.data
          if (m.data.retcode != 0) {
            this.notify(m.data.retmsg);
            return;
          }
          if(!sick.is_cf_reg) {
            this.$router.push({ path: "/home" });
          }else {
            this.$router.push({ path: "/billcase", query: {cid: sick.medical_id, caseindex: 1, casesize: 13} });
          }
          
        }).catch(err=>{
          this.$dialog.loading.close()
          this.$dialog.alert({mes: JSON.stringify(err)})
        })
    },
    notify(info) {
      this.$dialog.notify({
        mes: info,
        timeout: 1500
      });
    }
  },
  mounted() {
    this.init();
  },
  components: { ydLsBox, ydLsLayout, ydLsButton }
};
</script>
